﻿@page "/forwarding"
@using JT808Gateway.Data.Entity
@inject JT808Gateway.Data.ForwardingServerRepository forwardingServerRepository
@inject JT808Gateway.Data.ForwardingServerBindingRepository forwardingServerBindingRepository

<div>
    <h6>转发服务器:</h6>
    <Table TItem="ForwardingServer"
       IsPagination="true" PageItemsSource="_pageItemSource"
       IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowSkeleton="true"
       OnQueryAsync="OnQuery_ForwardingServer" OnSaveAsync="OnSaveAsync_ForwardingServer" OnDeleteAsync="OnDeleteAsync_ForwardingServer"
    >
        <TableColumns>
            <TableColumn @bind-Field="@context.Name" Text="名称"></TableColumn>
            <TableColumn @bind-Field="@context.IPAddress" Text="IP地址"></TableColumn>
            <TableColumn @bind-Field="@context.Port" Text="端口"></TableColumn>
            <TableColumn @bind-Field="@context.NetType" Text="类型"></TableColumn>
        </TableColumns>
    </Table>
</div>

<div style="margin-top:15px">
    <h6>转发绑定:</h6>
    <Table TItem="ForwardingServerBinding"
        IsPagination="true" PageItemsSource="_pageItemSource"
       IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowSkeleton="true"
       OnQueryAsync="OnQuery_ForwardingServerBinding" OnSaveAsync="OnSaveAsync_ForwardingServerBinding" OnDeleteAsync="OnDeleteAsync_ForwardingServerBinding"
    >
        <TableColumns>
            <TableColumn @bind-Field="@context.SimNumber" Text="Sim"></TableColumn>
            <TableColumn @bind-Field="@context.ServerId" Visible="false">
                <EditTemplate Context="value">
                     <div class="col-12 col-sm-6">
                        <Select DisplayText="转发服务器" TValue="int?"  @bind-Value="@context.ServerId" OnSelectedItemChanged="OnSelectedItemChanged">
                            <Options>
                                <SelectOption Text="<Null>" Value="0" Active="false"></SelectOption>
                                @foreach(var s in forwardingServerRepository.Query())
                                {
                                    <SelectOption Text="@s.Name" Value="@s.Id.ToString()" Active="@(((ForwardingServerBinding)value).ServerId == s.Id)"></SelectOption>
                                }
                            </Options>
                        </Select>
                    </div>
                </EditTemplate>
            </TableColumn>
            <TableColumn @bind-Field="@context.ForwardingServer" Text="转发服务器">
                <EditTemplate Context="value"></EditTemplate>
                <Template Context="value">
                    <div>@value.Value?.Name</div>
                </Template>
            </TableColumn>   
            <TableColumn @bind-Field="@context.Enabled" Text="已启用">
                <Template Context="value">
                    @if (value.Value)
                    {
                    <div class="text-success">已启用</div>
                }
                else
                {
                    <div class="text-danger">未启用</div>
                }
                </Template>
            </TableColumn>
        </TableColumns>
    </Table>
</div>

@code {
    private List<SelectedItem> _serverItems = null;
    private SelectedItem _selectedServer = null;

    private Task OnSelectedItemChanged(SelectedItem item)
    {
        _selectedServer = item;
        return Task.CompletedTask;
    }


    #region 绑定

    private int[] _pageItemSource = new int[] { 10,25,50 };

    async Task<QueryData<ForwardingServerBinding>> OnQuery_ForwardingServerBinding(QueryPageOptions options)
    {
        var queryData = new QueryData<ForwardingServerBinding>();
        RefAsync<int> total = new RefAsync<int>();
        var bindings = await forwardingServerBindingRepository.QueryPage(options.PageIndex, options.PageItems, total);

        queryData.Items = bindings;
        queryData.TotalCount = total.Value;
        return queryData;
    }

    async Task<bool> OnDeleteAsync_ForwardingServerBinding(IEnumerable<ForwardingServerBinding> items)
    {
        var succ = await forwardingServerBindingRepository.DeleteBatchAsync(items.ToList());
        return succ;
    }

    async Task<bool> OnSaveAsync_ForwardingServerBinding(ForwardingServerBinding item,ItemChangedType changedType)
    {
        int s = 0;
        if (int.TryParse(_selectedServer.Value, out s) && s!=0)
        {
            item.ServerId = s;
        }
        else { 
            item.ServerId = null;
        }
        return await forwardingServerBindingRepository.SaveAsync(item);
    }
    #endregion

    #region 服务器

    async Task<QueryData<ForwardingServer>> OnQuery_ForwardingServer(QueryPageOptions options)
    {
        var servers = await forwardingServerRepository.QueryPage(null, options.PageIndex, options.PageItems);
        var queryData = new QueryData<ForwardingServer>();
        queryData.Items = servers.data;
        queryData.TotalCount = servers.dataCount;
        return queryData;
    }

    async Task<bool> OnDeleteAsync_ForwardingServer(IEnumerable<ForwardingServer> items)
    {
        var succ = await forwardingServerRepository.DeleteBatchAsync(items.ToList());
        return succ;
    }

    async Task<bool> OnSaveAsync_ForwardingServer(ForwardingServer item,ItemChangedType changedType)
    {
       return await forwardingServerRepository.SaveAsync(item);
    }

    #endregion
}
